package com.t.bricks.bussiness.db.dao.loginlog.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.loginlog.LoginLogDao;
import com.t.bricks.bussiness.db.entity.loginlog.LoginLog;
import com.t.bricks.bussiness.db.mapper.loginlog.LoginLogMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class LoginLogDaoImpl implements LoginLogDao {
	
	@Autowired
	private LoginLogMapper loginLogMapper;

	/**
	 * 简约方式添加登录日志
	 * @param sIp 登录者IP
	 * @param sMACAddress 登录者使用的MAC地址
	 * @param sNameOrNo 登录者使用的用户名或工号
	 * @return
	 */
	@Override
	public MsgEmity add(String sIp, String sMACAddress, String sNameOrNo) {
		LoginLog loginLog = new LoginLog();
		loginLog.setsIp(sIp);
		loginLog.setsMACAddress(sMACAddress);
		loginLog.setsName(sNameOrNo);

		try {
			int i = loginLogMapper.addLog(loginLog);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", loginLog);
	}
	
}
